<?
class Pager {
	var $count;
	var $select;
	var $pagesize;
	var $default_pagesize=10;
	var $pages_visible=7;
	var $current_page;
	var $field='pager';
	var $template='pager';
	var $renderAdmin=true; // где брать темплейт
	var $def_sortby="pos";
	var $def_sortdir=""; // (или desc)
	var $primary_sortby=""; // например, сортировка по папкам isfolder
	var $primary_sortdir="";
	var $hasPos=true;
	var $dataSource;
	var $useDataSource=false;
	var $primary_table_prefix; // if we use 2 or more tables in query
	
	function Pager($select='')
	{
		$this->pagesize=$this->default_pagesize;
		$field=$this->field;
		if($_REQUEST[$field]=="showall") $this->pagesize=1000000;
		else  $this->current_page = $_REQUEST[$field];
		if($this->current_page=="")$this->current_page=1;
		if($select)
		{
			$this->select = $select;
			$p1= strpos($select,"select ")+7;
			$p2= strpos($select," from ");
			$count_select = substr($select,0,$p1)."count(*)".substr($select,$p2);
			$res = mysql_query($count_select);
			$row = mysql_fetch_array($res);
			$this->count=$row[0];
		}
		else $this->useDataSource=true;
	}
	
	
	function GetRows()
	{
		$out_res = Array();
		$i=0;
		if(!$this->useDataSource)
		{
			$res = mysql_query($this->select.($this->renderAdmin ? $this->GetOrderCond():'')." limit ".($this->pagesize*($this->current_page-1)).",".($this->pagesize) );
			$i=$this->pagesize*($this->current_page-1);
			while($row = mysql_fetch_array($res))
			{
				if($_GET["sortdir"]=="desc") $row["row_number"]=$this->count-$i;
				else $row["row_number"]=$i+1;
				$out_res[] = $row;
				$i++;
			}
		}
		else {
			$this->count=sizeof($this->dataSource);
			while($i<$this->count)
			{
				$row = $this->dataSource[$i];
				$row["row_number"]=$i+1;
				if( $i >= ($this->pagesize*$this->current_page) ) break;
				if( $i >= $this->pagesize*($this->current_page-1) ) $out_res[] = $row;
				
				$i++;
			}
		}
		return $out_res;
	}


function GetOrderCond()	{
		// pos - обозначение того что используем сортировку по умолчанию
		if($_GET["sortby"] && $_GET["sortby"]!="pos") $sortby=$_GET["sortby"];
		else $sortby=$this->def_sortby;
		if($_GET["sortdir"]) $sortdir=$_GET["sortdir"];
		else $sortdir="asc";
		
		if(!$this->hasPos && $sortby=="pos") $sortby=($this->primary_table_prefix?$this->primary_table_prefix.'.':'')."id";
		
		if($sortby==$this->def_sortby && $this->def_sortdir=="desc") $sortdir = ($sortdir=="desc"?"asc":"desc");
		
		if($this->primary_sortby)
			return " order by $this->primary_sortby $this->primary_sortdir, $sortby $sortdir, ".($this->primary_table_prefix?$this->primary_table_prefix.'.':'')."id $sortdir";
		else
			return " order by $sortby $sortdir, ".($this->primary_table_prefix?$this->primary_table_prefix.'.':'')."id $sortdir";
	}



	function RenderPager()
	{
		$half=floor($this->pages_visible/2);
		$pages = ceil( $this->count/$this->pagesize);
		
		$count_before = $this->current_page-1;
		$count_after = $pages-$this->current_page;
		
		if( $count_before + $count_after  >= $this->pages_visible)
		{
			if(min($count_before,$count_after)<$half )
			{
			if($count_before <= $count_after) {$show_left = min($half,$count_before); $show_right=$this->pages_visible-$show_left-1;}
			else {$show_right = min($half,$count_after); $show_left=$this->pages_visible-$show_right-1;}
			}
			else {$show_left = $half; $show_right=$this->pages_visible-$show_left-1;}
		}
		else
		{
			$show_left  =	$count_before;
			$show_right = 	$count_after;
		}
		if($show_left+$show_right+1 > $this->pages_visible) if($show_right>$show_left) $show_right-=1; else $show_left-=1;
		
		// start templating
		if($this->renderAdmin)
			$temp = GetAdminTemplate($this->template);
		else
			$temp = GetSiteTemplate($this->template.'/'.$this->template);
		
		$dao=Array();
		$dao["url"]=LinkBuilder($this->field.'=');
		$dao["countall"]=$this->count;
		$dao["countpages"]=$pages;
		$dao["default_pagesize"]=$this->default_pagesize;
		if($this->count > $this->pagesize)          $dao["showall"]     = LinkBuilder($this->field.'=showall');
		if($show_left<$count_before)                $dao["left_more"]   = LinkBuilder($this->field.'='.($this->current_page-$show_left-1));
		if($show_right<$count_after)                $dao["right_more"]  = LinkBuilder($this->field.'='.($this->current_page+$show_right+1));
		if($this->pagesize>$this->default_pagesize) $dao["showed_much"] = 1;
		if($this->current_page>1)                   $dao["prev"]        = LinkBuilder($this->field.'='.($this->current_page-1));
		if($this->current_page<$pages)              $dao["next"]        = LinkBuilder($this->field.'='.($this->current_page+1));
		if($this->current_page-$show_left-1>0)      $dao["first"]       = LinkBuilder($this->field.'=1');
		if($this->current_page+$show_right+1<$pages)      $dao["last"]        = LinkBuilder($this->field.'='.$pages);
		
		$dao["pages"]=Array();
		for($i=$this->current_page-$show_left; $i<=$this->current_page+$show_right; $i++)
		{
			$newpage=Array();
			$newpage["title"] = $i;
			$newpage["url"] = LinkBuilder($this->field."=".$i);
			if($i==$this->current_page) $newpage["active"]=1;
			$dao["pages"][]=$newpage;
		}
		
		return TemplaterParse($temp, $dao);
	}
	
	
	function GetPagesCount()
	{
		return ceil( $this->count/$this->pagesize);
	}
}
















